package com.spzx.product.controller;

import com.github.pagehelper.PageHelper;
import com.spzx.domain.Brand;
import com.spzx.product.service.IBrandService;
import com.spzx.vo.R;
import com.spzx.vo.Result;
import com.spzx.vo.ResultCodeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 品牌Controller
 */
@Tag(name = "品牌接口管理")
@RestController
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private IBrandService brandService;

    @Operation(summary = "添加品牌")
    @PostMapping("add")
    public Result<Integer> add(@RequestBody  Brand brand) {

        //返回影响行数
        int rows = brandService.addBrand(brand);
        if (rows == 0){
            return Result.build(rows, ResultCodeEnum.FAIL);
        }
        //return rows > 0 ? AjaxResult.success() : AjaxResult.error();
        return Result.build(rows, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "修改品牌")
    @PutMapping("update")
    public Result<Integer> update(@RequestBody Brand brand) {

        int rows = brandService.update(brand);
        if (rows == 0){
            return Result.build(rows, ResultCodeEnum.FAIL);
        }
        //return rows > 0 ? AjaxResult.success() : AjaxResult.error();
        return Result.build(rows, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "获取品牌详细信息")
    @GetMapping("getBrand/{id}")
    public Result<Brand> getBrand(@PathVariable Long id) {
        Brand brand = null;
        try {
            brand = brandService.getBrand(id);
        } catch (Exception e) {
            return Result.build(brand, ResultCodeEnum.FAIL);
        }
        return Result.build(brand, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "查询品牌列表")
    @GetMapping("selectAll")
    public Result<List<Brand>> getBrandAll() {
        return Result.build(brandService.selectBrandAll(),ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "查询品牌列表")
    @GetMapping("findPage/{page}/{limit}")
    public Result<List<Brand>> list(@PathVariable int page,@PathVariable int limit) {
        //PageHelper   当前页2  每页2
        //1 查询所有数据  10
        //2 根据设置分页参数，返回对应数据
        //设置分页参数
        PageHelper.startPage(page,limit);
        return Result.build(brandService.selectBrandAll(),ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "删除")
    @DeleteMapping("remove/{ids}")
    public Result<Integer> remove(@PathVariable Long[] ids) {
        return Result.build(brandService.deleteBrandByIds(ids),ResultCodeEnum.SUCCESS);
    }

}
